<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateShopAutosendUpdate extends Migrator
{
    /**
     * 商城 自动发货更新
     */
    public function up()
    {
        // 商品表
        $table = $this->table('shop_goods');
        $table->addColumn(Column::enum('type', ['normal', 'virtual', 'card'])->setDefault('normal')->setAfter('id')->setComment('商品类型:normal=实体商品,virtual=虚拟商品,card=电子卡密'))
            ->addColumn('dispatch_type', 'string', ['limit' => 120, 'null' => true, 'after' => 'service_ids', 'default' => null, 'comment' => '发货方式'])
            ->update();

        // 订单表
        $table = $this->table('shop_order_item');
        $table->addColumn(Column::enum('goods_type', ['normal', 'virtual', 'card'])->setDefault('normal')->setAfter('goods_id')->setComment('商品类型:normal=实体商品,virtual=虚拟商品,card=电子卡密'))
            ->update();

        // dispatch
        $table = $this->table('shop_dispatch');
        $table->changeColumn(Column::enum('type', ['express', 'autosend'])->setDefault('express')->setComment('发货方式:express=快递物流,autosend=自动发货'))
            ->update();

        $table = $this->table('shop_dispatch_autosend', ['comment' => '自动发货', 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci']);
        $table->addColumn('dispatch_id', 'integer', ['limit'  =>  11, 'default' => 0, 'comment' => '配送模板'])
            ->addColumn(Column::enum('type', ['text', 'params'])->setDefault('text')->setComment('自动发货类型:text=固定内容,params=自定义内容'))
            ->addColumn('content', 'string', ['limit'  =>  1200, 'null' => true, 'default' => null, 'comment' => '发货内容'])
            ->addColumn('create_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '更新时间'])
            ->create();
    }

    /**
     * Migrate Down.
     */
    public function down()
    {
        // 商品表
        $table = $this->table('shop_goods');
        $table->removeColumn('type')
            ->removeColumn('dispatch_type')
            ->update();

        // 订单表
        $table = $this->table('shop_order_item');
        $table->removeColumn('goods_type')
            ->update();

        // dispatch
        $table = $this->table('shop_dispatch');
        $table->changeColumn(Column::enum('type', ['express'])->setDefault('express')->setComment('发货方式:express=快递物流'))
            ->update();

        $table = $this->table('shop_dispatch_autosend');
        $table->drop();
    }
}
